// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';

export const asyncRoutes = [];
const routes = [
    {
        path: '/home',
        name: 'Home1',
        component: () => import('@/views/layout/index.vue'),
    },
    {
        path: '/',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
    },

    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('@/views/404.vue'),
    },
];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [...routes],
});
router.beforeEach((to, from, next) => {
    let permissionsString = localStorage.getItem('permissions') || '[]';

    let permissions: string[] = JSON.parse(permissionsString);
    if (to.path === '/login' || to.path === '/') {
        next();
    } else {
        let permission = to?.meta?.permission;
        if (permission && !permissions.includes(permission as string)) {
            // 如果permission不为空，且不在权限列表中，则跳转到404页面
            next({ name: 'NotFound' });
        } else {
            next();
        }
    }
});
export default router;
